<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>animationqueue.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_fx_animationqueue.js.html">animationqueue.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A class which automatically plays through a queue of
<a name="line17"></a> * animations.  AnimationParallelQueue and AnimationSerialQueue provide
<a name="line18"></a> * specific implementations of the abstract class AnimationQueue.
<a name="line19"></a> *
<a name="line20"></a> * @see ../demos/animationqueue.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.fx.AnimationParallelQueue&#39;);
<a name="line24"></a>goog.provide(&#39;goog.fx.AnimationQueue&#39;);
<a name="line25"></a>goog.provide(&#39;goog.fx.AnimationSerialQueue&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.array&#39;);
<a name="line28"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line30"></a>goog.require(&#39;goog.fx.Transition.EventType&#39;);
<a name="line31"></a>goog.require(&#39;goog.fx.TransitionBase&#39;);
<a name="line32"></a>goog.require(&#39;goog.fx.TransitionBase.State&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Constructor for AnimationQueue object.
<a name="line38"></a> *
<a name="line39"></a> * @constructor
<a name="line40"></a> * @extends {goog.fx.TransitionBase}
<a name="line41"></a> */
<a name="line42"></a>goog.fx.AnimationQueue = function() {
<a name="line43"></a>  goog.base(this);
<a name="line44"></a>
<a name="line45"></a>  /**
<a name="line46"></a>   * An array holding all animations in the queue.
<a name="line47"></a>   * @type {Array.&lt;goog.fx.TransitionBase&gt;}
<a name="line48"></a>   * @protected
<a name="line49"></a>   */
<a name="line50"></a>  this.queue = [];
<a name="line51"></a>};
<a name="line52"></a>goog.inherits(goog.fx.AnimationQueue, goog.fx.TransitionBase);
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Pushes an Animation to the end of the queue.
<a name="line57"></a> * @param {goog.fx.TransitionBase} animation The animation to add to the queue.
<a name="line58"></a> */
<a name="line59"></a>goog.fx.AnimationQueue.prototype.add = function(animation) {
<a name="line60"></a>  goog.asserts.assert(this.isStopped(),
<a name="line61"></a>      &#39;Not allowed to add animations to a running animation queue.&#39;);
<a name="line62"></a>
<a name="line63"></a>  if (goog.array.contains(this.queue, animation)) {
<a name="line64"></a>    return;
<a name="line65"></a>  }
<a name="line66"></a>
<a name="line67"></a>  this.queue.push(animation);
<a name="line68"></a>  goog.events.listen(animation, goog.fx.Transition.EventType.FINISH,
<a name="line69"></a>                     this.onAnimationFinish, false, this);
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Removes an Animation from the queue.
<a name="line75"></a> * @param {goog.fx.Animation} animation The animation to remove.
<a name="line76"></a> */
<a name="line77"></a>goog.fx.AnimationQueue.prototype.remove = function(animation) {
<a name="line78"></a>  goog.asserts.assert(this.isStopped(),
<a name="line79"></a>      &#39;Not allowed to remove animations from a running animation queue.&#39;);
<a name="line80"></a>
<a name="line81"></a>  if (goog.array.remove(this.queue, animation)) {
<a name="line82"></a>    goog.events.unlisten(animation, goog.fx.Transition.EventType.FINISH,
<a name="line83"></a>                         this.onAnimationFinish, false, this);
<a name="line84"></a>  }
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Handles the event that an animation has finished.
<a name="line90"></a> * @param {goog.events.Event} e The finishing event.
<a name="line91"></a> * @protected
<a name="line92"></a> */
<a name="line93"></a>goog.fx.AnimationQueue.prototype.onAnimationFinish = goog.abstractMethod;
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Disposes of the animations.
<a name="line98"></a> * @override
<a name="line99"></a> */
<a name="line100"></a>goog.fx.AnimationQueue.prototype.disposeInternal = function() {
<a name="line101"></a>  goog.array.forEach(this.queue, function(animation) {
<a name="line102"></a>    animation.dispose();
<a name="line103"></a>  });
<a name="line104"></a>  this.queue.length = 0;
<a name="line105"></a>
<a name="line106"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Constructor for AnimationParallelQueue object.
<a name="line113"></a> * @constructor
<a name="line114"></a> * @extends {goog.fx.AnimationQueue}
<a name="line115"></a> */
<a name="line116"></a>goog.fx.AnimationParallelQueue = function() {
<a name="line117"></a>  goog.base(this);
<a name="line118"></a>
<a name="line119"></a>  /**
<a name="line120"></a>   * Number of finished animations.
<a name="line121"></a>   * @type {number}
<a name="line122"></a>   * @private
<a name="line123"></a>   */
<a name="line124"></a>  this.finishedCounter_ = 0;
<a name="line125"></a>};
<a name="line126"></a>goog.inherits(goog.fx.AnimationParallelQueue, goog.fx.AnimationQueue);
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/** @inheritDoc */
<a name="line130"></a>goog.fx.AnimationParallelQueue.prototype.play = function(opt_restart) {
<a name="line131"></a>  if (this.queue.length == 0) {
<a name="line132"></a>    return false;
<a name="line133"></a>  }
<a name="line134"></a>
<a name="line135"></a>  if (opt_restart || this.isStopped()) {
<a name="line136"></a>    this.finishedCounter_ = 0;
<a name="line137"></a>    this.onBegin();
<a name="line138"></a>  } else if (this.isPlaying()) {
<a name="line139"></a>    return false;
<a name="line140"></a>  }
<a name="line141"></a>
<a name="line142"></a>  this.onPlay();
<a name="line143"></a>  if (this.isPaused()) {
<a name="line144"></a>    this.onResume();
<a name="line145"></a>  }
<a name="line146"></a>  var resuming = this.isPaused() &amp;&amp; !opt_restart;
<a name="line147"></a>
<a name="line148"></a>  this.startTime = goog.now();
<a name="line149"></a>  this.endTime = null;
<a name="line150"></a>  this.setStatePlaying();
<a name="line151"></a>
<a name="line152"></a>  goog.array.forEach(this.queue, function(anim) {
<a name="line153"></a>    if (!resuming || anim.isPaused()) {
<a name="line154"></a>      anim.play(opt_restart);
<a name="line155"></a>    }
<a name="line156"></a>  });
<a name="line157"></a>
<a name="line158"></a>  return true;
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/** @inheritDoc */
<a name="line163"></a>goog.fx.AnimationParallelQueue.prototype.pause = function() {
<a name="line164"></a>  if (this.isPlaying()) {
<a name="line165"></a>    goog.array.forEach(this.queue, function(anim) {
<a name="line166"></a>      if (anim.isPlaying()) {
<a name="line167"></a>        anim.pause();
<a name="line168"></a>      }
<a name="line169"></a>    });
<a name="line170"></a>
<a name="line171"></a>    this.setStatePaused();
<a name="line172"></a>    this.onPause();
<a name="line173"></a>  }
<a name="line174"></a>};
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/** @inheritDoc */
<a name="line178"></a>goog.fx.AnimationParallelQueue.prototype.stop = function(opt_gotoEnd) {
<a name="line179"></a>  goog.array.forEach(this.queue, function(anim) {
<a name="line180"></a>    if (!anim.isStopped()) {
<a name="line181"></a>      anim.stop(opt_gotoEnd);
<a name="line182"></a>    }
<a name="line183"></a>  });
<a name="line184"></a>
<a name="line185"></a>  this.setStateStopped();
<a name="line186"></a>  this.endTime = goog.now();
<a name="line187"></a>
<a name="line188"></a>  this.onStop();
<a name="line189"></a>  this.onEnd();
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/** @inheritDoc */
<a name="line194"></a>goog.fx.AnimationParallelQueue.prototype.onAnimationFinish = function(e) {
<a name="line195"></a>  this.finishedCounter_++;
<a name="line196"></a>  if (this.finishedCounter_ == this.queue.length) {
<a name="line197"></a>    this.endTime = goog.now();
<a name="line198"></a>
<a name="line199"></a>    this.setStateStopped();
<a name="line200"></a>
<a name="line201"></a>    this.onFinish();
<a name="line202"></a>    this.onEnd();
<a name="line203"></a>  }
<a name="line204"></a>};
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Constructor for AnimationSerialQueue object.
<a name="line210"></a> * @constructor
<a name="line211"></a> * @extends {goog.fx.AnimationQueue}
<a name="line212"></a> */
<a name="line213"></a>goog.fx.AnimationSerialQueue = function() {
<a name="line214"></a>  goog.base(this);
<a name="line215"></a>
<a name="line216"></a>  /**
<a name="line217"></a>   * Current animation in queue currently active.
<a name="line218"></a>   * @type {number}
<a name="line219"></a>   * @private
<a name="line220"></a>   */
<a name="line221"></a>  this.current_ = 0;
<a name="line222"></a>};
<a name="line223"></a>goog.inherits(goog.fx.AnimationSerialQueue, goog.fx.AnimationQueue);
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/** @inheritDoc */
<a name="line227"></a>goog.fx.AnimationSerialQueue.prototype.play = function(opt_restart) {
<a name="line228"></a>  if (this.queue.length == 0) {
<a name="line229"></a>    return false;
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  if (opt_restart || this.isStopped()) {
<a name="line233"></a>    if (this.current_ &lt; this.queue.length &amp;&amp;
<a name="line234"></a>        !this.queue[this.current_].isStopped()) {
<a name="line235"></a>      this.queue[this.current_].stop(false);
<a name="line236"></a>    }
<a name="line237"></a>
<a name="line238"></a>    this.current_ = 0;
<a name="line239"></a>    this.onBegin();
<a name="line240"></a>  } else if (this.isPlaying()) {
<a name="line241"></a>    return false;
<a name="line242"></a>  }
<a name="line243"></a>
<a name="line244"></a>  this.onPlay();
<a name="line245"></a>  if (this.isPaused()) {
<a name="line246"></a>    this.onResume();
<a name="line247"></a>  }
<a name="line248"></a>
<a name="line249"></a>  this.startTime = goog.now();
<a name="line250"></a>  this.endTime = null;
<a name="line251"></a>  this.setStatePlaying();
<a name="line252"></a>
<a name="line253"></a>  this.queue[this.current_].play(opt_restart);
<a name="line254"></a>
<a name="line255"></a>  return true;
<a name="line256"></a>};
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/** @inheritDoc */
<a name="line260"></a>goog.fx.AnimationSerialQueue.prototype.pause = function() {
<a name="line261"></a>  if (this.isPlaying()) {
<a name="line262"></a>    this.queue[this.current_].pause();
<a name="line263"></a>    this.setStatePaused();
<a name="line264"></a>    this.onPause();
<a name="line265"></a>  }
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/** @inheritDoc */
<a name="line270"></a>goog.fx.AnimationSerialQueue.prototype.stop = function(opt_gotoEnd) {
<a name="line271"></a>  this.setStateStopped();
<a name="line272"></a>  this.endTime = goog.now();
<a name="line273"></a>
<a name="line274"></a>  if (opt_gotoEnd) {
<a name="line275"></a>    for (var i = this.current_; i &lt; this.queue.length; ++i) {
<a name="line276"></a>      var anim = this.queue[i];
<a name="line277"></a>      // If the animation is stopped, start it to initiate rendering.  This
<a name="line278"></a>      // might be needed to make the next line work.
<a name="line279"></a>      if (anim.isStopped()) anim.play();
<a name="line280"></a>      // If the animation is not done, stop it and go to the end state of the
<a name="line281"></a>      // animation.
<a name="line282"></a>      if (!anim.isStopped()) anim.stop(true);
<a name="line283"></a>    }
<a name="line284"></a>  } else if (this.current_ &lt; this.queue.length) {
<a name="line285"></a>    this.queue[this.current_].stop(false);
<a name="line286"></a>  }
<a name="line287"></a>
<a name="line288"></a>  this.onStop();
<a name="line289"></a>  this.onEnd();
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/** @inheritDoc */
<a name="line294"></a>goog.fx.AnimationSerialQueue.prototype.onAnimationFinish = function(e) {
<a name="line295"></a>  if (this.isPlaying()) {
<a name="line296"></a>    this.current_++;
<a name="line297"></a>    if (this.current_ &lt; this.queue.length) {
<a name="line298"></a>      this.queue[this.current_].play();
<a name="line299"></a>    } else {
<a name="line300"></a>      this.endTime = goog.now();
<a name="line301"></a>      this.setStateStopped();
<a name="line302"></a>
<a name="line303"></a>      this.onFinish();
<a name="line304"></a>      this.onEnd();
<a name="line305"></a>    }
<a name="line306"></a>  }
<a name="line307"></a>};
</pre>


</body>
</html>
